#region License

// Another free, open solution by someone who gets it.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;

using Patterns.Validation.Framework;

namespace Patterns.Validation.Advanced
{
	/// <summary>
	/// 	Contains a number of extension methods for floating point <see cref = "NumericValidator{T}" />.
	/// </summary>
	public static class FloatingPointExtensions
	{
		/// <summary>
		/// 	Validates a floating point value approximates another value as close as possible.
		/// </summary>
		/// <param name = "parent">The <see cref = "NumericValidator{T}" /> object that is being extended.</param>
		/// <param name = "expectedValue">
		/// 	The expected value to compare the actual value with.
		/// </param>
		/// <param name = "precision">
		/// 	The maximum amount of which the two values may differ.
		/// </param>
		public static AndConstraint<NumericValidator<float?>> BeApproximately(this NumericValidator<float?> parent,
		                                                                      float expectedValue, float precision)
		{
			return BeApproximately(parent, expectedValue, precision, string.Empty);
		}

		/// <summary>
		/// 	Validates a floating point value approximates another value as close as possible.
		/// </summary>
		/// <param name = "parent">The <see cref = "NumericValidator{T}" /> object that is being extended.</param>
		/// <param name = "expectedValue">
		/// 	The expected value to compare the actual value with.
		/// </param>
		/// <param name = "precision">
		/// 	The maximum amount of which the two values may differ.
		/// </param>
		/// <param name = "reason">
		/// 	A formatted phrase as is supported by <see cref = "string.Format(string,object[])" /> explaining why the assertion 
		/// 	is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
		/// </param>
		/// <param name = "reasonArgs">
		/// 	Zero or more objects to format using the placeholders in <see cref = "reason" />.
		/// </param>
		public static AndConstraint<NumericValidator<float?>> BeApproximately(this NumericValidator<float?> parent,
		                                                                      float expectedValue, float precision, string reason,
		                                                                      params object[] reasonArgs)
		{
			Execute.Verification
				.ForCondition(parent.Subject != null)
				.BecauseOf(reason, reasonArgs)
				.FailWith("Expected value to approximate {0} +/- {1}{reason}, but it was <null>.", expectedValue, precision);

			var nonNullableValidator = new NumericValidator<float>(expectedValue);
			nonNullableValidator.BeApproximately(expectedValue, precision, reason, reasonArgs);

			return new AndConstraint<NumericValidator<float?>>(parent);
		}

		/// <summary>
		/// 	Validates a floating point value approximates another value as close as possible.
		/// </summary>
		/// <param name = "parent">The <see cref = "NumericValidator{T}" /> object that is being extended.</param>
		/// <param name = "expectedValue">
		/// 	The expected value to compare the actual value with.
		/// </param>
		/// <param name = "precision">
		/// 	The maximum amount of which the two values may differ.
		/// </param>
		public static AndConstraint<NumericValidator<float>> BeApproximately(this NumericValidator<float> parent,
		                                                                     float expectedValue, float precision)
		{
			return BeApproximately(parent, expectedValue, precision, string.Empty);
		}

		/// <summary>
		/// 	Validates a floating point value approximates another value as close as possible.
		/// </summary>
		/// <param name = "parent">The <see cref = "NumericValidator{T}" /> object that is being extended.</param>
		/// <param name = "expectedValue">
		/// 	The expected value to compare the actual value with.
		/// </param>
		/// <param name = "precision">
		/// 	The maximum amount of which the two values may differ.
		/// </param>
		/// <param name = "reason">
		/// 	A formatted phrase as is supported by <see cref = "string.Format(string,object[])" /> explaining why the assertion 
		/// 	is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
		/// </param>
		/// <param name = "reasonArgs">
		/// 	Zero or more objects to format using the placeholders in <see cref = "reason" />.
		/// </param>
		public static AndConstraint<NumericValidator<float>> BeApproximately(this NumericValidator<float> parent,
		                                                                     float expectedValue, float precision, string reason,
		                                                                     params object[] reasonArgs)
		{
			float actualDifference = Math.Abs(expectedValue - (float) parent.Subject);

			if (actualDifference > precision)
			{
				Execute.Verification
					.BecauseOf(reason, reasonArgs)
					.FailWith("Expected value {0} to approximate {1} +/- {2}{reason}, but it differed by {3}.",
					          parent.Subject, expectedValue, precision, actualDifference);
			}

			return new AndConstraint<NumericValidator<float>>(parent);
		}

		/// <summary>
		/// 	Validates a floating point value approximates another value as close as possible.
		/// </summary>
		/// <param name = "parent">The <see cref = "NumericValidator{T}" /> object that is being extended.</param>
		/// <param name = "expectedValue">
		/// 	The expected value to compare the actual value with.
		/// </param>
		/// <param name = "precision">
		/// 	The maximum amount of which the two values may differ.
		/// </param>
		public static AndConstraint<NumericValidator<double?>> BeApproximately(this NumericValidator<double?> parent,
		                                                                       double expectedValue, double precision)
		{
			return BeApproximately(parent, expectedValue, precision, string.Empty);
		}

		/// <summary>
		/// 	Validates a floating point value approximates another value as close as possible.
		/// </summary>
		/// <param name = "parent">The <see cref = "NumericValidator{T}" /> object that is being extended.</param>
		/// <param name = "expectedValue">
		/// 	The expected value to compare the actual value with.
		/// </param>
		/// <param name = "precision">
		/// 	The maximum amount of which the two values may differ.
		/// </param>
		/// <param name = "reason">
		/// 	A formatted phrase as is supported by <see cref = "string.Format(string,object[])" /> explaining why the assertion 
		/// 	is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
		/// </param>
		/// <param name = "reasonArgs">
		/// 	Zero or more objects to format using the placeholders in <see cref = "reason" />.
		/// </param>
		public static AndConstraint<NumericValidator<double?>> BeApproximately(this NumericValidator<double?> parent,
		                                                                       double expectedValue, double precision, string reason,
		                                                                       params object[] reasonArgs)
		{
			Execute.Verification
				.ForCondition(parent.Subject != null)
				.BecauseOf(reason, reasonArgs)
				.FailWith("Expected value to approximate {0} +/- {1}{reason}, but it was <null>.", expectedValue, precision);

			var nonNullableValidator = new NumericValidator<double>(expectedValue);
			BeApproximately(nonNullableValidator, expectedValue, precision, reason, reasonArgs);

			return new AndConstraint<NumericValidator<double?>>(parent);
		}

		/// <summary>
		/// 	Validates a floating point value approximates another value as close as possible.
		/// </summary>
		/// <param name = "parent">The <see cref = "NumericValidator{T}" /> object that is being extended.</param>
		/// <param name = "expectedValue">
		/// 	The expected value to compare the actual value with.
		/// </param>
		/// <param name = "precision">
		/// 	The maximum amount of which the two values may differ.
		/// </param>
		public static AndConstraint<NumericValidator<double>> BeApproximately(this NumericValidator<double> parent,
		                                                                      double expectedValue, double precision)
		{
			return BeApproximately(parent, expectedValue, precision, string.Empty);
		}

		/// <summary>
		/// 	Validates a floating point value approximates another value as close as possible.
		/// </summary>
		/// <param name = "parent">The <see cref = "NumericValidator{T}" /> object that is being extended.</param>
		/// <param name = "expectedValue">
		/// 	The expected value to compare the actual value with.
		/// </param>
		/// <param name = "precision">
		/// 	The maximum amount of which the two values may differ.
		/// </param>
		/// <param name = "reason">
		/// 	A formatted phrase as is supported by <see cref = "string.Format(string,object[])" /> explaining why the assertion 
		/// 	is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
		/// </param>
		/// <param name = "reasonArgs">
		/// 	Zero or more objects to format using the placeholders in <see cref = "reason" />.
		/// </param>
		public static AndConstraint<NumericValidator<double>> BeApproximately(this NumericValidator<double> parent,
		                                                                      double expectedValue, double precision, string reason,
		                                                                      params object[] reasonArgs)
		{
			double actualDifference = Math.Abs(expectedValue - (double) parent.Subject);

			Execute.Verification
				.ForCondition(actualDifference <= precision)
				.BecauseOf(reason, reasonArgs)
				.FailWith("Expected value {0} to approximate {1} +/- {2}{reason}, but it differed by {3}.",
				          parent.Subject, expectedValue, precision, actualDifference);

			return new AndConstraint<NumericValidator<double>>(parent);
		}
	}
}